$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'quoteFactoryMaterial/quotefactorymaterial/list',
        datatype: "json",
        colModel: [
            { label: 'id', name: 'id', index: 'id', width: 50, key: true, align: 'center'},
            {label: '材料', name: 'optionName', index: 'optionName', width: 80, align: 'center'},
            {label: 'optionKey', name: 'optionKey', index: 'optionKey', width: 50, hidden: true},
            {label: '工厂id', name: 'factoryId', index: 'factory_id', width: 30, align: 'center', hidden: true},
            {label: '工厂名字', name: 'sellerName', index: 'seller_name', width: 80, align: 'center'},
            {
                label: '特种纸张', name: 'specialtyPaper', index: 'is_specialty_paper', width: 80, align: 'center',
                formatter: function (value, options, row) {
                    return value == true ? '是' : '否'
                }
            },
            {label: '创建时间', name: 'createDate', index: 'create_dte', width: 80, align: 'center'},
            // { label: '创建人', name: 'createUser', index: 'create_user', width: 80,align: 'center'},
            {
                label: '启用/禁用', name: 'display', index: 'display', width: 50, align: 'center',
                formatter: function (value, options, row) {

                    if (value == 1) {
                        var actions = [];
                        actions.push('<a href="javascript:void(0);" onclick="vm.setStart(' + row.id + ',' + value + ');">禁用</a>');
                        return actions.join(' ');
                    } else if (value == 0) {
                        var actions = [];
                        actions.push('<a href="javascript:void(0);" onclick="vm.setStart(' + row.id + ',' + value + ');">启用</a>');
                        return actions.join(' ');
                    }
                }
            }
        ],
        viewrecords: true,
        height: 555,
        rowNum: 20,
        rowList: [20, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        updateShow: false,
        addShow:false,
        title: null,
        quoteFactoryMaterial: {},
        quoteAttributeOptionList: [],  //选项集合
        materialPriceList: [],  //材料价格表
        materialOption: '',
        q: {
            optionName: ''
        },
        updateBtn: true,
        leftTableData:[],
        tableData: [],
        addMaterialView:false,
        addMaterialViewListL:[],
        multipleSelection:[],
        paperStatus:'',
        disable:false,
    },
    watch: {
        materialOption(val, oldVal) {
            if (val != '') {
                vm.quoteFactoryMaterial.optionKey = val;
                factoryMaterialTable(val)
            }
        },

    },
    //页面加载时获取数据
    mounted() {
        $.get(baseURL + 'quoteFactoryMaterial/quotefactorymaterial/getLeftTableData', function (r) {
            vm.leftTableData=r;
        })
    },
    methods: {
        //弹出窗口,获取所有基础材料
        addMaterial() {
            $.get(baseURL + "quoteFactoryMaterial/quotefactorymaterial/getMaterialList/", function (r) {
                vm.quoteAttributeOptionList = r;
            })
            vm.addMaterialView=true
        },
        //显示克重
        handleaddMaterial(row, event, column) {
            $.get(baseURL + 'quoteFactoryMaterial/quotefactorymaterial/addWeight/' + row.optionName, function (r) {
                vm.addMaterialViewListL = r;
            })
        },
        //复选框状态改变
        changeFun(val) {
            vm.multipleSelection = val;
        },
        //添加材料
        addToLeftTable() {
            vm.disable=true
            $.ajax({
                type: "POST",
                url: baseURL + 'quoteFactoryMaterial/quotefactorymaterial/save',
                contentType: "application/json",
                data: JSON.stringify(vm.multipleSelection),
                success: function (r) {
                    if (r.code === 0) {
                        vm.reload();
                        vm.disable=false
                        vm.addMaterialView=false
                        alert('操作成功');
                    } else {
                        alert(r.msg);
                        vm.disable=false
                    }
                }
            });

        },
        handleCurrentChange(row, event, column) {
            $.get(baseURL + 'quoteFactoryMaterial/quotefactorymaterial/addWeight/' + row.optionName, function (r) {
                vm.tableData = r;
            })
            vm.paperStatus=row.specialtyPaper
            vm.getInfo(row.id)
        },
        tableDataChange(row, event, column) {

        },
        handleEdit(index, row) {
            // console.log(index, row);
        },
        handleDelete(index, row) {
            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: baseURL + "quoteFactoryMaterial/quotefactorymaterial/delWeight",
                    datatype: "json",
                    data: {"optionName":row.optionName},
                    success: function (r) {
                        if (r.code == 0) {
                            vm.reload();
                            alert('删除成功')
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        detail(id) {
            var rowData = $("#jqGrid").jqGrid("getRowData", id);
            vm.showList = false;
            vm.title = "查看";
            vm.updateBtn = false
            vm.getInfo(id)
            vm.materialOption = rowData.optionKey
            factoryMaterialTable(rowData.optionKey)
        },
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.tableData = []
            vm.showList = false;
            vm.updateShow=false;
            vm.addShow = true;
            vm.title = "新增";
            vm.quoteFactoryMaterial = {
                isSpecialtyPaper: 0,
                floatingCoefficient: 1,
                materialPrice: null
            };
            vm.materialOption = '',
                $.get(baseURL + "quoteAttribute/quoteattributeoption/getMaterialList/", function (r) {
                    vm.quoteAttributeOptionList = r;
                })
        },
        update: function (event) {
            var id = getSelectedRow();
            if (id == null) {
                return;
            }
            var rowData = $("#jqGrid").jqGrid("getRowData", id);
            vm.showList = false;
            vm.updateShow = true;


            vm.getInfo(id)
            vm.materialOption =
                vm.title = rowData.optionName+"配价";
            // factoryMaterialTable(rowData.optionKey)



        },
        saveOrUpdate: function (event) {
            //用于保存编辑后的单元格的数据
            vm.materialPriceList = []
            vm.quoteFactoryMaterial.materialPriceList = JSON.stringify(vm.tableData);
            //为没有id的材料设置特种纸的状态
            vm.tableData.forEach(v=>{
                if (!v.id && vm.paperStatus) {
                    v.isSpecialtyPaper=1
                }
            })
            var url = "quoteFactoryMaterial/quotefactorymaterial/update";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.tableData),
                success: function (r) {
                    if (r.code === 0) {
                        $.get(baseURL + 'quoteFactoryMaterial/quotefactorymaterial/addWeight/' + vm.quoteFactoryMaterial.optionName, function (r) {
                            vm.tableData = r;
                        })
                        alert('操作成功');
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRows();
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: baseURL + "quoteFactoryMaterial/quotefactorymaterial/delete",
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getInfo: function (id) {
            $.get(baseURL + "quoteFactoryMaterial/quotefactorymaterial/info/" + id, function (r) {
                vm.quoteFactoryMaterial = r.quoteFactoryMaterial;
            });
        },
        reload: function (event) {
            vm.showList=true
            vm.updateShow=false
            $.get(baseURL + 'quoteFactoryMaterial/quotefactorymaterial/getLeftTableData', function (r) {
                vm.leftTableData=r;
            })
        },
        setStart: function (id, value) {
            var url = "quoteFactoryMaterial/quotefactorymaterial/setStart/" + id + "/" + value;
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: {},
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        specialPaper(index,row) {
            $.get(baseURL + 'quoteFactoryMaterial/quotefactorymaterial/addWeight/' + row.optionName, function (r) {
                vm.tableData = r;
                vm.tableData.forEach(v=>{
                    v.specialtyPaper=row.specialtyPaper
                })
                const params= vm.tableData.filter(v=>v.id!=null)
                let data=[]
                params.forEach(v=>{
                    data.push(v.id)
                })
                let url=''
                if (row.specialtyPaper) {
                    url='quoteFactoryMaterial/quotefactorymaterial/specialPaper'
                }else{
                    url='quoteFactoryMaterial/quotefactorymaterial/unSpecialPaper'
                }
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify(data),
                    success: function (r) {
                        if (r.code === 0) {
                            alert('操作成功');
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            })
        }
    }
});

